package com.i360day.redis.configuration;

import com.i360day.redis.aspect.GlobalRepeatSubmitAdvice;
import com.i360day.redis.aspect.RepeatSubmitAspect;
import com.i360day.redis.exception.PointcutException;
import com.i360day.redis.properties.RedisProperties;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.ObjectUtils;

@Configuration
@Import(GlobalRepeatSubmitAdvice.class)
@EnableConfigurationProperties(value = RedisProperties.class)
public class RedisSubmitConfiguration {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public AspectJExpressionPointcutAdvisor configurabledvisor(GlobalRepeatSubmitAdvice globalRepeatSubmitAdvice) {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        if (ObjectUtils.isEmpty(redisProperties.getGlobalRepeatSubmit().getPointcut())) {
            throw new PointcutException();
        }
        advisor.setExpression(redisProperties.getGlobalRepeatSubmit().getPointcut());
        advisor.setAdvice(globalRepeatSubmitAdvice);
        return advisor;
    }

    /**
     * <p> @Author liju.z <p>
     *
     * <p> @Description 全局重复提交 <p>
     *
     * <p> @Date  16:43 <p>
     *
     * <p> @Param [] <p>
     *
     * <p> @return [] <p>
     **/
    @Bean
    public GlobalRepeatSubmitAdvice globalRepeatSubmitAdvice() {
        return new GlobalRepeatSubmitAdvice();
    }


    /**
     * <p> @Author liju.z <p>
     *
     * <p> @Description 添加重复提交标记 <p>
     *
     * <p> @Date  16:43 <p>
     **/
    @Bean
    public RepeatSubmitAspect repeatSubmitAspect() {
        return new RepeatSubmitAspect();
    }

}
